tools/pygrub: Fix TOCTOU race introduced by c/s 63dcc68
authorAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 29 Oct 2014 14:09:41 +0000 (14:09 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 4 Nov 2014 10:03:31 +0000 (10:03 +0000)
In addition, use os.makedirs() which will also create intermediate directories
if they don't exist.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Olaf Hering <olaf@aepfle.de>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
tools/pygrub/src/pygrub

index 4cc846fa1798421edb1b07f04b5c7367210fcd18..aa7e562e8a404f23a9d0ffdf058b550b03b4e52d 100644 (file)
@@ -13,7 +13,7 @@
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-import os, sys, string, struct, tempfile, re, traceback, stat
+import os, sys, string, struct, tempfile, re, traceback, stat, errno
 import copy
 import logging
 import platform
@@ -851,8 +851,14 @@ if __name__ == "__main__":
     if debug:
        logging.basicConfig(level=logging.DEBUG)
 
-    if not os.path.isdir(output_directory):
-        os.mkdir(output_directory, 0700)
+
+    try:
+        os.makedirs(output_directory, 0700)
+    except OSError,e:
+        if (e.errno == errno.EEXIST) and os.path.isdir(output_directory):
+            pass
+        else:
+            raise
 
     if output is None or output == "-":
         fd = sys.stdout.fileno()